package com.study_online.service.Impl;


import com.study_online.domain.AdminNotice;
import com.study_online.domain.AdminVideo;
import com.study_online.domain.ResultInfo;
import com.study_online.domain.User;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study_online.domain.*;
import com.study_online.mapper.UserMapper;
import com.study_online.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 孙远帅
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 查询用户所有信息
     *
     * @return
     */
    @Override
    public ResultInfo selectAll(String tablename) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            List<User> list = userMapper.selectList("user");
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public ResultInfo login(String username, String password) {
        ResultInfo info = new ResultInfo();
        User user = userMapper.login(username, password);
        if (user != null) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorInfo("账户或密码错误");
        }
        info.setData(user);
        return info;
    }

    /**
     * @param account
     * @param password
     * @return
     */
    @Override
    public ResultInfo logining(String account, String password) {
        ResultInfo info = new ResultInfo();
        User user = userMapper.logining(account, password);
        if (user != null) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorInfo("账户或密码错误");
        }
        info.setData(user);
        return info;
    }

    /**
     * 邮箱查询
     *
     * @param email
     * @return
     */
    @Override
    public ResultInfo findEmail(String email) {
        ResultInfo info = new ResultInfo();
        info.setFlag(false);
        User user = userMapper.findEmail(email);
        if (user != null) {
            info.setFlag(true);
            info.setErrorInfo("邮箱存在");
        }
        return info;
    }

    /**
     * 判断用户名是否存在
     * @param username
     * @return
     */
    @Override
    public ResultInfo findUsername(String username) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        User user = userMapper.findUsername(username);
        if (user != null) {
            info.setFlag(false);
            info.setErrorInfo("用户名存在,重新输入注册");
        }
        return info;
    }

    /**
     * 用户注册
     *
     * @param user
     * @param code
     * @return
     */
    @Override
    public ResultInfo register(User user, String code) {
        ResultInfo info = new ResultInfo();
        System.out.println(user);
        try {
            userMapper.register(user);
            info.setFlag(true);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("邮箱已被注册！");
        }
        return info;
    }

    /**
     * 首页视频展示
     *
     * @return
     */
    @Override
    public ResultInfo selectVideoList(List<Integer> videoIds) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        List<Video> list = new ArrayList<>();
        try {
            //根据传入的videoId集合依次查询数据
            for (Integer videoId : videoIds) {
                Video video = userMapper.selectVideoList(videoId);
//                System.out.println(video);
                list.add(video);
            }
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查询展示视频失败");
            e.printStackTrace();
        }
        System.out.println(info);
        return info;
    }

    /**
     * 首页公告展示
     *
     * @return
     */
    @Override
    public ResultInfo selectNoticeList(List<Integer> noticeIds) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        List<Notice> list = new ArrayList<>();
        try {
            //根据传入的videoId集合依次查询数据
            for (Integer noticeId : noticeIds) {
                Notice notice = userMapper.selectNoticeList(noticeId);
                list.add(notice);
            }

            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查询展示公告失败");
            e.printStackTrace();
        }
        return info;
    }



    @Override
    public ResultInfo selectHomeVideo() {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            List<Video> videos = userMapper.selectHomeVideo();
            info.setData(videos);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

}